import UIKit
class XMThread: Thread {
    deinit {
        print(XMThread.self,#function)
    }
}
class MsgRunLoopCtr_123globalvan: UIViewController {
    lazy var xmThreadStoped = false
    lazy var xmThread:XMThread = XMThread.init {[weak self] in
        print("--thread begin--")
        RunLoop.current.add(Port.init(), forMode: .default)
        while let stoped = self?.xmThreadStoped {
            if !stoped {
                RunLoop.current.run(mode: .default, before: .distantFuture)
            }
        }
        print("--thread end--")
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .white
        title = "runloop"
        xmThread.start()
    }
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        perform(#selector(self.threadDoSomeThing), on: xmThread, with: nil, waitUntilDone: false)
        print("touchesBegan")
    }
    @objc private func threadDoSomeThing() {
        print(Thread.current,"threadDoSomeThings")
    }
    @objc private func threadStop() {
        print("threadStop")
        self.xmThreadStoped = true
        CFRunLoopStop(CFRunLoopGetCurrent())
    }
    deinit{
        print(MsgRunLoopCtr_123globalvan.self,#function)
        perform(#selector(self.threadStop), on: xmThread, with: nil, waitUntilDone: true)
    }
}
